package com.github.ecbp.user.service.controller.front;


import com.github.ecbp.common.api.CommonResult;
import com.github.ecbp.common.data.builder.CommonPageBuilder;
import com.github.ecbp.common.data.controller.BaseController;
import com.github.ecbp.common.req.BaseIdDTO;
import com.github.ecbp.common.req.BaseIdListDTO;
import com.github.ecbp.common.req.SimpleIdListStatusParam;
import com.github.ecbp.common.resp.NameVo;
import com.github.ecbp.user.service.dto.MenuBo;
import com.github.ecbp.user.service.dto.RoleDto;
import com.github.ecbp.user.service.dto.RoleQueryDto;
import com.github.ecbp.user.service.response.RoleRes;
import com.github.ecbp.user.service.service.MenuService;
import com.github.ecbp.user.service.service.RoleService;
import com.github.ecbp.user.service.vo.ListInfoVo;
import com.github.ecbp.user.service.vo.MenuNode;
import com.github.ecbp.user.service.vo.RolePageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.github.ecbp.common.constant.CacheKeyConstant.PROJECT_NAME;

/**
 * <p>
 * 后台用户角色表 前端控制器
 * </p>
 *
 * @author zj2626
 * @since 2020-03-18
 */
@RequestMapping("/api/" + PROJECT_NAME + "/role")
@Api(tags = "后台角色管理")
@RestController
public class RoleController extends BaseController {

    @Autowired
    protected RoleService service;

    @Autowired
    private MenuService menuService;

    @ApiOperation(value = "所有可选数据下拉列表", notes = "zhangj", response = NameVo.class)
    @PostMapping(value = "/listAll")
    public CommonResult getItem() {
        return CommonResult.succeed(service.listIdAndName());
    }

    @ApiOperation(value = "添加角色", notes = "zhangj")
    @PostMapping(value = "/create")
    public CommonResult create(@Validated @RequestBody RoleDto param) {
        service.create(param);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "修改角色", notes = "zhangj")
    @PostMapping(value = "/update")
    public CommonResult update(@Validated(BaseIdDTO.NotNullId.class) @RequestBody RoleDto param) {
        service.updateSelectiveById(param);
        reloadPermission(Collections.singleton(param.getId()));
        return CommonResult.succeed();
    }

    @ApiOperation(value = "分页列表-角色", notes = "zhangj", response = RolePageVo.class)
    @PostMapping(value = "/selectPage")
    public CommonResult selectPage(@Validated @RequestBody RoleQueryDto param) {
        return CommonResult.succeed(CommonPageBuilder.build(service.page(param, RolePageVo.class)));
    }

    @ApiOperation(value = "分页列表数据-用户数", notes = "zhangj", response = ListInfoVo.class)
    @PostMapping(value = "/pageInfoList")
    public CommonResult pageInfoList(@Validated @RequestBody BaseIdListDTO param) {
        return CommonResult.succeed(service.pageInfoList(param.getIds()));
    }

    @ApiOperation(value = "修改角色状态", notes = "zhangj")
    @PostMapping(value = "/updateStatus")
    public CommonResult updateStatus(@Validated(BaseIdDTO.NotNullId.class) @RequestBody SimpleIdListStatusParam param) {
        service.updateStatusById(param);
        reloadPermission(new HashSet<>(param.getIds()));
        return CommonResult.succeed();
    }

    private void reloadPermission(Set<Long> ids) {
        if (!CollectionUtils.isEmpty(ids)) {
            service.reloadUserPermission(ids);
        }
    }

    @ApiOperation(value = "树形结构返回指定角色的所有菜单列表", notes = "zhangj", response = MenuNode.class)
    @PostMapping(value = "/treeList")
    public CommonResult treeList(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        List<MenuNode> list = menuService.treeList(new MenuBo(false, param.getId()));
        return CommonResult.succeed(list);
    }

    @ApiOperation(value = "树形结构返回指定角色的所有菜单列表+按钮", notes = "zhangj", response = MenuNode.class)
    @PostMapping(value = "/treeListWithButton")
    public CommonResult treeListWithButton(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        List<MenuNode> list = menuService.treeList(new MenuBo(true, param.getId()));
        return CommonResult.succeed(list);
    }

    @ApiOperation(value = "根据ID获取角色详情", notes = "zhangj", response = RoleRes.class)
    @PostMapping(value = "/info")
    public CommonResult getItem(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        return CommonResult.succeed(service.getById(param.getId()));
    }
}
